﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BanNhot
{
    public partial class frmMatHang : Form
    {
        public frmMatHang()
        {
            InitializeComponent();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        void loadData()
        {
            thanhttp_bannhotEntities bn = new thanhttp_bannhotEntities();
            var load = from g in bn.MatHangs select g;
            if (load != null)
            {
                dGVMatHang.DataSource = load.ToList();
            }
        }

        int m_MaMatHang=0;
        private void btnLuu_Click(object sender, EventArgs e)
        {
            thanhttp_bannhotEntities bn = new thanhttp_bannhotEntities();
            try
            {

                MatHang mh = bn.MatHangs.FirstOrDefault(i => i.id == m_MaMatHang);
                if (mh != null)
                {
                    mh.Ten = txtTen.Text;
                    mh.KyHieu = txtKyHieu.Text;
                    mh.MoTa = txtMoTa.Text;
                    mh.GiaBan = int.Parse(txtGiaBan.Text);
                    bn.SaveChanges();
                    MessageBox.Show("Lưu thành công.");
                    loadData();
                }
                else
                {
                    mh = new MatHang
                    {
                        Ten = txtTen.Text,
                        KyHieu = txtKyHieu.Text,
                        MoTa = txtMoTa.Text,
                        GiaBan = int.Parse(txtGiaBan.Text)
                    };
                    bn.MatHangs.Add(mh);
                    bn.SaveChanges();
                    MessageBox.Show("Lưu thành công.");
                    loadData();
                };
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.InnerException.ToString());
            }
        }

        private void frmMatHang_Load(object sender, EventArgs e)
        {
            loadData();
        }
    }
}
